/**
 * Project Name:Micro-Services-Config-SpringFramework
 * File Name   :SpringElOperatorAndRegExpression.java
 * Package Name:com.micro.services.config.springframework.el
 * Date:2015-9-7下午1:48:42
 * Copyright (c) 2015, http://my.oschina.net/httpssl All Rights Reserved.
 *
*/

package com.micro.services.config.springframework.el;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
/**
 * ClassName:SpringElOperatorAndRegExpression <br/>
 * Function :TODO ADD FUNCTION. <br/>
 * Reason	:TODO ADD REASON. <br/>
 * Date     :2015-9-7 下午1:48:42 <br/>
 * @author  :http://my.oschina.net/httpssl
 * @email   :491835898@QQ.COM
 * @since   :JDK 1.7
 * @see 	 
 */
public class SpringElOperatorAndRegExpression
{
	   // 表达式解析对象  
	static ExpressionParser exp = null;   
    // 标准赋值上下文  
    static StandardEvaluationContext secontext;  

    public static Object sayHelloForSpEl(String express) {  
        ExpressionParser exp = new SpelExpressionParser();  
        Expression ex = exp.parseExpression(express);  
        return ex.getValue();  
    }  
    
    public void testSpElOperatorAndRegExpression() throws Exception {  
        // 关系操作  
        assertTrue(exp.parseExpression("1 == 1").getValue(Boolean.class));  
        assertTrue(exp.parseExpression("1 eq 1").getValue(Boolean.class));  
        assertTrue(exp.parseExpression("1 > -1").getValue(Boolean.class));  
        assertTrue(exp.parseExpression("1 gt -1").getValue(Boolean.class));  
        assertTrue(exp.parseExpression("'a' < 'b'").getValue(Boolean.class));  
        assertTrue(exp.parseExpression("'a' lt 'b'").getValue(Boolean.class));  
        assertTrue(exp.parseExpression(  
                " new Integer(123) instanceof T(Integer) ").getValue(  
                Boolean.class));  
        assertTrue(exp.parseExpression("'5.00' matches '^-?//d+(//.//d{2})?$'")  
                .getValue(Boolean.class));  
        // 逻辑操作  
        assertTrue(exp.parseExpression("true and true").getValue(Boolean.class));  
        assertTrue(exp.parseExpression("true or false").getValue(Boolean.class));  
        assertFalse(exp.parseExpression("innerClass.isGt30ForAge() and false ")  
                .getValue(secontext, Boolean.class));  
        assertFalse(exp.parseExpression("!innerClass.isGt30ForAge() and true ")  
                .getValue(secontext, Boolean.class));  
        assertTrue(exp.parseExpression("!false").getValue(Boolean.class));  
        // 运算操作  
        assertEquals(2, exp.parseExpression("1 + 1").getValue());  
        assertEquals("ABab",  
                exp.parseExpression("'AB' + 'ab'").getValue(String.class));  
        assertEquals(25.0,  
                exp.parseExpression("1 + 2 * 8 div 4 mod 2 + 2 ^ 3 * 3e0")  
                        .getValue());  
        assertEquals(exp.parseExpression("1 + 2 * 8 / 4 % 2 + 2 ^ 3 ")  
                .getValue(),  
                exp.parseExpression("1 + 2 * 8 div 4 mod 2 + 2 ^ 3 ")  
                        .getValue());  
    }  

    
    public static void main(String[] args) {
    	secontext = new StandardEvaluationContext(null);  
        exp = new SpelExpressionParser();  
	}
	
}